# Guess, Andrew and Alexander Coppock. Does Counter-Attitudinal Information Cause Backlash? Results from Three Large Survey Experiments. British Journal of Political Science, forthcoming.

# Produces Figure 2

rm(list = ls())

library(tidyverse)
library(dbarts)
library(ggpubr)

load("GC_study_1.rdata")
load("GC_Study_2.rdata")
load("GC_Study_3.rdata")
source("GC_helpers.R")

# Study 1 - Gun Control ---------------------------------------------------


study_1 <- 
  study_1 %>% 
  select(W2_scale, W2_Q1, pro, Z_information, ppagecat, PPREG4, PPEDUCAT, hispanic, PPGENDER, PPINCIMP, PPMARIT, working, XPARTY7, IDEO, weight2) %>% 
  na.omit()

df1 <- study_1 %>% mutate(Z_information = "control")
df2 <- study_1 %>% mutate(Z_information = "pro_information")
df3 <- study_1 %>% mutate(Z_information = "con_information")

bart_df <- bind_rows(df1, df2, df3)
bart_df$Z_information <- as.factor(bart_df$Z_information)

# 1_1
bart_model <- dbarts(formula = W2_scale ~ pro + Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working + as.factor(XPARTY7) + as.factor(IDEO), weights = weight2, test = bart_df, data = study_1)
bart_fit_1_1 <- bart_model$run()

# 1_2

bart_model <- dbarts(formula = W2_Q1 ~ pro + Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working + as.factor(XPARTY7) + as.factor(IDEO), weights = weight2, test = bart_df, data = study_1)
bart_fit_1_2 <- bart_model$run()

# Study 2: Minimum Wage ---------------------------------------------------

study_2 <-
  study_2 %>% 
  select(amount_T2, favor_T2_recode, positive_information, negative_information, condition_type, initial_position, favor_T1_recode, age, male, ideology, partyid, education, amount_T1) %>%
  mutate(placebo_dummy = (condition_type == "Placebo")) %>%
  na.omit()

df1 <- study_2 %>% mutate(positive_information = 0, negative_information = 0, placebo_dummy = FALSE)
df2 <- study_2 %>% mutate(positive_information = 1, negative_information = 0, placebo_dummy = FALSE)
df3 <- study_2 %>% mutate(positive_information = 0, negative_information = 1, placebo_dummy = FALSE)

bart_df <- bind_rows(df1, df2, df3)


# 2_1
bart_model <- dbarts(formula = amount_T2 ~ positive_information + negative_information + placebo_dummy + initial_position + favor_T1_recode +
                       age + male + ideology + partyid + education + amount_T1, test = bart_df, data = study_2)
bart_fit_2_1 <- bart_model$run()

# 2_2
bart_model <- dbarts(formula = favor_T2_recode ~ positive_information + negative_information + placebo_dummy + initial_position + favor_T1_recode +
                       age + male + ideology + partyid + education + amount_T1, test = bart_df, data = study_2)
bart_fit_2_2 <- bart_model$run()


# Study 3: Capital Punishment ---------------------------------------------

study_3 <-
  study_3 %>% 
  select(support_recode_T2, deter_recode_T2, positive_information, negative_information, condition_factor, support_recode_T1, deter_recode_T1, age, female, ideology, educ, race, pro) %>%
  mutate(nn_dummy = (condition_factor == "NN")) %>%
  na.omit()

df1 <- study_3 %>% mutate(positive_information = 0, negative_information = 0, nn_dummy = FALSE)
df2 <- study_3 %>% mutate(positive_information = 2, negative_information = 0, nn_dummy = FALSE)
df3 <- study_3 %>% mutate(positive_information = 0, negative_information = 2, nn_dummy = FALSE)

bart_df <- bind_rows(df1, df2, df3)


# 3_1
bart_model <- dbarts(formula = support_recode_T2 ~ positive_information + negative_information + nn_dummy + support_recode_T1 + deter_recode_T1 + age + female + ideology + educ + race + pro, test = bart_df, data = study_3)
bart_fit_3_1 <- bart_model$run()

# 3_2
bart_model <- dbarts(formula = deter_recode_T2 ~ positive_information + negative_information + nn_dummy + support_recode_T1 + deter_recode_T1 + age + female + ideology + educ + race + pro, test = bart_df, data = study_3)
bart_fit_3_2 <- bart_model$run()


# prop_correct_figs -------------------------------------------------------

study_2 <-
  within(study_2,{
    pro <- rep(NA, nrow(study_2))
    pro[initial_position == "pro_raise"] <- 1
    pro[initial_position == "inconsistent_raise"] <- 0
  })


prep_bart_fit <-
  function(bart_fit, study_df) {
    n <- nrow(study_df)
    bart_test <- plyr::alply(bart_fit$test, 3)
    bart_test <- do.call(cbind, bart_test)
    pos_mat <- bart_test[(n + 1):(2 * n),] - bart_test[1:n,]
    neg_mat <-
      bart_test[((2 * n) + 1):(3 * n),] - bart_test[1:n,]
    
    gg_pos <-
      data_frame(
        tau_hat = apply(pos_mat, 1, mean),
        ci_upper = apply(pos_mat, 1, quantile, 0.975),
        ci_lower = apply(pos_mat, 1, quantile, 0.025),
        ordering = rank(tau_hat, ties.method = "first"), 
        treatment = "Positive Information",
        pro = study_df$pro
      )
    
    gg_neg <-
      data_frame(
        tau_hat = apply(neg_mat, 1, mean),
        ci_upper = apply(neg_mat, 1, quantile, 0.975),
        ci_lower = apply(neg_mat, 1, quantile, 0.025),
        ordering = rank(tau_hat, ties.method = "first"), 
        treatment = "Negative Information",
        pro = study_df$pro
      )
    
    gg_df <- bind_rows(gg_pos, gg_neg)
  }

g_base <-
  ggplot(data = NULL, aes(tau_hat, ordering)) +
  geom_point(alpha = .05) +
  geom_errorbarh(aes(xmin = ci_lower, xmax = ci_upper),
                 alpha = .05,
                 height = 0) +
  geom_vline(xintercept = 0, lty = 2) +
  xlab("Estimated Treatment Effect") +
  facet_wrap( ~ treatment) +
  theme_bw() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    strip.background = element_blank(),
    panel.grid.minor.y = element_blank(), 
    panel.grid.major.x = element_blank(),
    plot.title = element_text(hjust = 0.5)
  ) 

df_1_1 <- prep_bart_fit(bart_fit_1_1, study_df = study_1)
df_1_2 <- prep_bart_fit(bart_fit_1_2, study_df = study_1)
df_2_1 <- prep_bart_fit(bart_fit_2_1, study_df = study_2)
df_2_2 <- prep_bart_fit(bart_fit_2_2, study_df = study_2)
df_3_1 <- prep_bart_fit(bart_fit_3_1, study_df = study_3)
df_3_2 <- prep_bart_fit(bart_fit_3_2, study_df = study_3)

g_1_1 <- g_base %+% df_1_1 + ggtitle("Study 1: Composite Scale")
g_1_2 <- g_base %+% df_1_2 + ggtitle("Study 1: Support Gun Control")
g_2_1 <- g_base %+% df_2_1 + ggtitle("Study 2: T2 Amount")
g_2_2 <- g_base %+% df_2_2 + ggtitle("Study 2: T2 Favor")
g_3_1 <- g_base %+% df_3_1 + ggtitle("Study 3: T2 Attitude Toward Capital Punishment")
g_3_2 <- g_base %+% df_3_2 + ggtitle("Study 3: T2 Belief in Deterrent Effect")


g_all <- ggarrange(g_1_1, g_1_2, g_2_1, g_2_2, g_3_1, g_3_2, labels = rep("", 6), ncol = 2, nrow = 3)
g_all

# In Text Figures ---------------------------------------------------------

mega_df <- 
  bind_rows(
    df_1_1 = df_1_1,
    df_1_2 = df_1_2,
    df_2_1 = df_2_1,
    df_2_2 = df_2_2,
    df_3_1 = df_3_1,
    df_3_2 = df_3_2, 
    .id = "study_dv"
  )

mega_df %>%
  group_by(treatment, study_dv) %>%
  summarize(pos = mean(tau_hat > 0),
            neg = mean(tau_hat < 0))



